﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.StockModule
{
    public class StockInDetailSpecification : ISpecification<StockInOutDetail, StockInOutDetailSearch>
    {
        #region ISpecification<StockInDetail,StockInDetailSearch> 成员

        public StockInOutDetailSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<StockInDetail> 成员

        public System.Linq.Expressions.Expression<Func<StockInOutDetail, bool>> Expression
        {
            get
            {
                IQueryBuilder<StockInOutDetail> queryBuilder = QueryBuilder.Create<StockInOutDetail>();

                if (SearchParams.IsNotNull())
                {
                    if (SearchParams.StockInCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(sid => sid.StockIn.Id, SearchParams.StockInCode);
                    }
                    if (SearchParams.HasLeft)
                    {
                        queryBuilder = queryBuilder.Greater(sid => sid.LeftNumber, 0);
                    }
                    if (SearchParams.StockCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(sid => sid.Stock.Id, SearchParams.StockCode);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
